package com.kawaiior.asoulworld.items.weapon;

import com.kawaiior.asoulworld.events.ASEventHelper;
import com.kawaiior.asoulworld.items.AsWeaponBase;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import javax.annotation.Nullable;
import java.util.List;

public class ItemKiraCow extends AsWeaponBase {

    public ItemKiraCow(String name) {
        super(name);
        this.attack_damage=7f;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
        if (worldIn.isRemote){
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }

        playerIn.playSound(SoundEvents.BLOCK_ANVIL_LAND, 1f, 0.8f);
        playerIn.setActiveHand(handIn);
        Entity pointedEntity = getPlayerLookTarget(worldIn, playerIn);
        if (pointedEntity instanceof EntityLiving) {
            EntityLiving living = (EntityLiving) pointedEntity;
            for (int i = 0; i < 64; i++) {
                if (ASEventHelper.teleportRandomly(living.posX,living.posY,living.posZ,worldIn,living)){
                    playerIn.getCooldownTracker().setCooldown(this, 300);
                    return super.onItemRightClick(worldIn, playerIn, handIn);
                }
            }
            playerIn.getCooldownTracker().setCooldown(this, 300);
            worldIn.createExplosion(playerIn, living.posX, living.posY+2, living.posZ, 2, false);
        }

        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Nullable
    private Entity getPlayerLookTarget(World world, EntityLivingBase living) {
        Entity pointedEntity = null;
        double range = 64D;
        Vec3d srcVec = new Vec3d(living.posX, living.posY + living.getEyeHeight(), living.posZ);
        Vec3d lookVec = living.getLook(1.0F);
        Vec3d destVec = srcVec.addVector(lookVec.x*range,lookVec.y*range,lookVec.z*range);
        float var9 = 1.0F;
        List<Entity> possibleList = world.getEntitiesWithinAABBExcludingEntity(living, living.getEntityBoundingBox().expand(lookVec.x * range, lookVec.y * range, lookVec.z * range).grow(var9, var9, var9));
        double hitDist = 0;

        for (Entity possibleEntity : possibleList) {
            if (possibleEntity.canBeCollidedWith()) {
                float borderSize = possibleEntity.getCollisionBorderSize();
                AxisAlignedBB collisionBB = possibleEntity.getEntityBoundingBox().grow((double) borderSize, (double) borderSize, (double) borderSize);
                RayTraceResult interceptPos = collisionBB.calculateIntercept(srcVec, destVec);

                if (collisionBB.contains(srcVec)) {
                    if (0.0D < hitDist || hitDist == 0.0D) {
                        pointedEntity = possibleEntity;
                        hitDist = 0.0D;
                    }
                } else if (interceptPos != null) {
                    double possibleDist = srcVec.distanceTo(interceptPos.hitVec);

                    if (possibleDist < hitDist || hitDist == 0.0D) {
                        pointedEntity = possibleEntity;
                        hitDist = possibleDist;
                    }
                }
            }
        }
        return pointedEntity;
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag) {
        tooltip.add("勇敢牛牛，不怕困难！");
    }
}
